cmake_minimum_required(VERSION 3.28)
project(RayCi)

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "C://Qt//6.5.3//msvc2019_64//lib//cmake")

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        PrintSupport
        Network
        OpenGL
        OpenGLWidgets
        DataVisualization
        REQUIRED)

# OpenCV库
set(OpenCV_DIR "D://raman_sw//opencv//build//x64//vc16//lib")
find_package(OpenCV 4.7.0  REQUIRED)

# freeglut库
include_directories("D://raman_sw//freeglut-3.2.2//install//include")
link_directories("D://raman_sw//freeglut-3.2.2//install//lib")

# boost库
include_directories("D://raman_sw//boost//include")
link_directories("D://raman_sw//boost//libs")

# RayCi库
include_directories("C://CINOGY//RayCi Pro SDK//C//Header")
link_directories("C://CINOGY//RayCi Pro SDK//C//Lib//x64")
set(RAYCI_BIN_DIR "C://CINOGY//RayCi Pro SDK//C//Bin//x64")

# FMC4030库
include_directories("D://raman_sw//FMC4030//include")
link_directories("D://raman_sw//FMC4030//lib")
set(FMC4030_BIN_DIR "D://raman_sw//FMC4030//bin")
#
##YAML库
#add_compile_definitions(YAML_CPP_STATIC_DEFINE)
#include_directories("D://raman_sw//raman_sw//YAML_CPP//include")
#
#if(MSVC AND CMAKE_BUILD_TYPE MATCHES "debug")
#    message(STATUS "aaaaaDebug")
#    link_directories("D://raman_sw//raman_sw//YAML_CPP//lib//debug")
#else()
#    message(STATUS "aaaaaRelease")
#    link_directories("D://raman_sw//raman_sw//YAML_CPP//lib//release")
#endif ()



set(QRC_SOURCE_FILES qdarkstyle/style.qrc)

add_executable(RayCi  main.cpp
        raycimainwindow.cpp
        raycimainwindow.h
        raycimainwindow.ui
        RayCi/LiveMode.cpp
        RayCi/LiveMode.h
        qcustomplot.h
        qcustomplot.cpp
        ${QRC_SOURCE_FILES}
        ${SOURCE_FILES}
        Single/single_2d_view.cpp
        Single/single_2d_view.h
        Single/single_2d_view.ui
        Single/single_options.cpp
        Single/single_options.h
        Single/single_options.ui
        Common/SurfaceGraphModifier.cpp
        Common/SurfaceGraphModifier.h
        Common/SurfaceGraph.cpp
        Common/SurfaceGraph.h
        Common/beamresults.cpp
        Common/beamresults.h
        Common/beamresults.ui
        Common/crosssection.cpp
        Common/crosssection.h
        Common/crosssection.ui
        Common/histogram.cpp
        Common/histogram.h
        Common/histogram.ui
        Common/powerprofile.cpp
        Common/powerprofile.h
        Common/powerprofile.ui
        Common/threed_view.cpp
        Common/threed_view.h
        Common/threed_view.ui
        Common/centroiddistances.cpp
        Common/centroiddistances.h
        Common/centroiddistances.ui
        LiveMode/livemode_options.cpp
        LiveMode/livemode_options.h
        LiveMode/livemode_options.ui
        Common/graphcontrol.cpp
        Common/graphcontrol.h
        Common/graphcontrol.ui
        LiveMode/livemode_2d_view.cpp
        LiveMode/livemode_2d_view.h
        LiveMode/livemode_2d_view.ui
        LiveMode/LiveModeClient.cpp
        LiveMode/LiveModeClient.h
        utils/TimeM.cpp
        utils/TimeM.h
        utils/utils.h
        utils/utils.cpp
        utils/threadsafe_linkedqueue.h
        utils/threadsafe_linkedqueue.cpp
        utils/Ordered.h
        utils/Ordered.cpp
        utils/Observer.h
        utils/Observer.cpp
        utils/interruptible_thread.h
        utils/interruptible_thread.cpp
        utils/RayCiDataReceiveQueueObserver.cpp
        utils/RayCiDataReceiveQueueObserver.h
        utils/AbstractQueueObserver.cpp
        utils/AbstractQueueObserver.h
        utils/EventListener.h
        utils/RayCiDataReceiveListener.cpp
        utils/RayCiDataReceiveListener.h
        utils/CCDImageData.cpp
        utils/CCDImageData.h
        utils/IGeneralCalibration.cpp
        utils/IGeneralCalibration.h
        utils/LiveModeOptionsListener.cpp
        utils/LiveModeOptionsListener.h
        utils/ThreeDViewListener.cpp
        utils/ThreeDViewListener.h
        utils/DataProcessor.cpp
        utils/DataProcessor.h
        utils/lock_free_queue.hpp
        Common/aoisetting.cpp
        Common/aoisetting.h
        Common/aoisetting.ui
        utils/RayCiExposureTimeListener.cpp
        utils/RayCiExposureTimeListener.h
        utils/RayCiExposureTimeQueueObserver.cpp
        utils/RayCiExposureTimeQueueObserver.h
        Common/beamprofilerdynamic.cpp
        Common/beamprofilerdynamic.h
        Common/beamprofilerdynamic.ui
        Common/ThreeD_View_Modifier.cpp
        Common/ThreeD_View_Modifier.h
        utils/DynamicProgressBar.cpp
        utils/DynamicProgressBar.h
        utils/PowerProfileDataReceiveListener.cpp
        utils/PowerProfileDataReceiveListener.h
        utils/PowerProfileDataReceiveQueueObserver.cpp
        utils/PowerProfileDataReceiveQueueObserver.h
        utils/PowerProfileData.cpp
        utils/PowerProfileData.h
        LiveMode/livemode_powerprofile_settings.cpp
        LiveMode/livemode_powerprofile_settings.h
        LiveMode/livemode_powerprofile_settings.ui
        RayCi/LiveMode_PowerProfiles.cpp
        RayCi/LiveMode_PowerProfiles.h
        LiveMode/livemode_powerprofile_options.cpp
        LiveMode/livemode_powerprofile_options.h
        LiveMode/livemode_powerprofile_options.ui
        LiveMode/livemode_crosssection_options.cpp
        LiveMode/livemode_crosssection_options.h
        LiveMode/livemode_crosssection_options.ui
        LiveMode/livemode_2d_view_settings.cpp
        LiveMode/livemode_2d_view_settings.h
        LiveMode/livemode_2d_view_settings.ui
        Log/logmessagewindow.cpp
        Log/logmessagewindow.h
        Log/logmessagewindow.ui
        utils/LogManager.cpp
        utils/LogManager.h
        RayCi/LiveMode_CrossSection.cpp
        RayCi/LiveMode_CrossSection.h
        utils/CrossSectionDataReceiveListener.cpp
        utils/CrossSectionDataReceiveListener.h
        utils/CrossSectionDataReceiveQueueObserver.cpp
        utils/CrossSectionDataReceiveQueueObserver.h
        utils/UnitConversion.cpp
        utils/UnitConversion.h
        RayCi/LiveMode_Measurement.cpp
        RayCi/LiveMode_Measurement.h
        RayCi/Single_Measurement.cpp
        RayCi/Single_Measurement.h
        RayCi/Single_Header.cpp
        RayCi/Single_Header.h
        Single/snapshot.cpp
        Single/snapshot.h
        Single/snapshot.ui
        RayCi/LiveMode_Background.cpp
        RayCi/LiveMode_Background.h
        Common/powerprofilebase.h
        RayCi/Single_CrossSection.cpp
        RayCi/Single_CrossSection.h
        RayCi/Single_PowerProfile.cpp
        RayCi/Single_PowerProfile.h
        RayCi/Single_CrossSectionUI.cpp
        RayCi/Single_CrossSectionUI.h
        Common/powerprofilebase.cpp
        Common/powerprofilebase.ui
        RayCi/Single_PowerProfileUI.cpp
        RayCi/Single_PowerProfileUI.h
        Stability/Beam/beam_stability_view.cpp
        Stability/Beam/beam_stability_view.h
        Stability/Beam/beam_stability_view.ui
        RayCi/Beam_Stability.cpp
        RayCi/Beam_Stability.h
        Common/crosssectionbase.cpp
        Common/crosssectionbase.h
        Common/crosssectionbase.ui
        RayCi/Beam_Quality.cpp
        RayCi/Beam_Quality.h
        RayCi/Time_Series.cpp
        RayCi/Time_Series.h
        TimeSeries/time_series_view.cpp
        TimeSeries/time_series_view.h
        TimeSeries/time_series_view.ui
        RayCi/Divergence.cpp
        RayCi/Divergence.h
        RayCi/LiveMode_Histogram.cpp
        RayCi/LiveMode_Histogram.h
        Stability/Pointing/pointing_stability_view.cpp
        Stability/Pointing/pointing_stability_view.h
        Stability/Pointing/pointing_stability_view.ui
        LiveMode/LiveMode_Analysis.cpp
        LiveMode/LiveMode_Analysis.h
        utils/HistogramDataReceiveQueueObserver.cpp
        utils/HistogramDataReceiveQueueObserver.h
        utils/HistogramDataReceiveListener.cpp
        utils/HistogramDataReceiveListener.h
        Single/Single_Histogram.cpp
        Single/Single_Histogram.h
        Common/histogrambase.cpp
        Common/histogrambase.h
        Common/histogrambase.ui
        Single/Single_HistogramUI.cpp
        Single/Single_HistogramUI.h
        Common/histogram_settings.cpp
        Common/histogram_settings.h
        Common/histogram_settings.ui
        Common/commonchart.cpp
        Common/commonchart.h
        Common/commonchart.ui
        RayCi/Pointing_Stability.cpp
        RayCi/Pointing_Stability.h
        Divergence/divergence_view.cpp
        Divergence/divergence_view.h
        Divergence/divergence_view.ui
        Common/common_2dview.cpp
        Common/common_2dview.h
        Common/common_2dview.ui
        BeamQuality/beam_quality_view.cpp
        BeamQuality/beam_quality_view.h
        BeamQuality/beam_quality_view.ui
        BeamLine/beam_line_view.cpp
        BeamLine/beam_line_view.h
        BeamLine/beam_line_view.ui
        RayCi/Beam_Line.cpp
        RayCi/Beam_Line.h
        Common/common_2dview_options.cpp
        Common/common_2dview_options.h
        Common/common_2dview_options.ui
        Common/common_chart_options.cpp
        Common/common_chart_options.h
        Common/common_chart_options.ui
        Divergence/divergence_result.cpp
        Divergence/divergence_result.h
        Divergence/divergence_result.ui
        BeamLine/beam_line_result.cpp
        BeamLine/beam_line_result.h
        BeamLine/beam_line_result.ui
        utils/BufferManager.cpp
        utils/BufferManager.h
        utils/LiveModeFloatData.cpp
        utils/LiveModeFloatData.h
        Single/snapshot_2d_view_settings.cpp
        Single/snapshot_2d_view_settings.h
        Single/snapshot_2d_view_settings.ui
        Single/single_2d_view_settings.cpp
        Single/single_2d_view_settings.h
        Single/single_2d_view_settings.ui
        TimeSeries/timeseries_2d_view_settings.cpp
        TimeSeries/timeseries_2d_view_settings.h
        TimeSeries/timeseries_2d_view_settings.ui
        Divergence/divergence_2d_view_settings.cpp
        Divergence/divergence_2d_view_settings.h
        Divergence/divergence_2d_view_settings.ui
        BeamLine/beamline_2d_view_settings.cpp
        BeamLine/beamline_2d_view_settings.h
        BeamLine/beamline_2d_view_settings.ui
        Stability/Beam/beamstability_2d_view_settings.cpp
        Stability/Beam/beamstability_2d_view_settings.h
        Stability/Beam/beamstability_2d_view_settings.ui
        Stability/Pointing/pointingstability_2d_view_settings.cpp
        Stability/Pointing/pointingstability_2d_view_settings.h
        Stability/Pointing/pointingstability_2d_view_settings.ui
        Single/snapshot_powerprofile_options.cpp
        Single/snapshot_powerprofile_options.h
        Single/snapshot_powerprofile_options.ui
        Common/common_statistics_chart.cpp
        Common/common_statistics_chart.h
        Common/common_statistics_chart.ui
        BeamQuality/Beam_Quality_Result.cpp
        BeamQuality/Beam_Quality_Result.h
        BeamQuality/Beam_Quality_Result.ui
        BeamQuality/beamquality_2d_view_settings.cpp
        BeamQuality/beamquality_2d_view_settings.h
        BeamQuality/BeamQuality_2D_View_Settings.ui
        BeamQuality/Beam_Quality_LaserWavelngth_View.h
        BeamQuality/Beam_Quality_LaserWavelngth_View.cpp
        Device_Motor/Device_Motor_Default.cpp
        Device_Motor/Device_Motor_Default.h
        Device_Motor/Device_Motor.cpp
        Device_Motor/Device_Motor.h
        Device_Motor/Device_Motor_Factory.h
        Device_Motor/Device_Motor_FuYu.cpp
        Device_Motor/Device_Motor_FuYu.h
        Device_Motor_Panel/Motor_Control_Panel.cpp
        Device_Motor_Panel/Motor_Control_Panel.h
        Device_Motor_Panel/CoordinateTransformStrategy.h
        Device_Motor_Panel/BeamQualityAnalysis.cpp
        Device_Motor_Panel/BeamQualityAnalysis.h
        Device_Motor_Panel/PresetPositionManager.cpp
        Device_Motor_Panel/PresetPositionManager.h
        Device_Motor_Panel/PositionConfigDialog.cpp
        Device_Motor_Panel/PositionConfigDialog.h
        Device_Motor_Panel/MotorConfig.cpp
        Device_Motor_Panel/MotorConfig.h
        config/motor_config.ini
        BeamQuality/ThreeD_Ellipse.hpp
        BeamQuality/ThreeDBeamQuality.cpp
        BeamQuality/ThreeDBeamQuality.h
        RayCiLut.hpp
        Common/commonlutwidget.cpp

)

qt_add_resources(${QRC_SOURCE_FILES})

# 链接OpenCV库
target_link_libraries(RayCi ${OpenCV_LIBS})

target_link_libraries(RayCi
        Qt::Core
        Qt::Gui
        Qt::Widgets
        Qt::DataVisualization
        Qt::PrintSupport
        Qt::Network
        Qt::OpenGL
        Qt::OpenGLWidgets
        freeglut
        freeglut_static
        libboost_system-vc143-mt-x64-1_84
        RayCiPro_C
        FMC4030-DLL
)
#
#if(MSVC AND CMAKE_BUILD_TYPE MATCHES "debug")
#    # debug模式
#    message(STATUS "debug")
#    target_link_libraries(RayCi yaml-cppd)
#else()
#    message(STATUS "release")
#    target_link_libraries(RayCi yaml-cpp)
#endif ()
# 定义配置文件的源路径
set(CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/config/motor_config.ini")

# 根据构建模式动态设置目标路径
if(CMAKE_BUILD_TYPE STREQUAL "debug")
    set(CONFIG_DESTINATION_DIR "${CMAKE_CURRENT_BINARY_DIR}/Debug/config")
else()
    set(CONFIG_DESTINATION_DIR "${CMAKE_CURRENT_BINARY_DIR}/Release/config")
endif()

# 创建目标目录（如果不存在）
file(MAKE_DIRECTORY ${CONFIG_DESTINATION_DIR})

# 添加自定义命令，在构建时复制配置文件
add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CONFIG_FILE}
        ${CONFIG_DESTINATION_DIR}/motor_config.ini
        COMMENT "Copying motor_config.ini to ${CONFIG_DESTINATION_DIR}"
)
set(RayCi_resource_files
        "data/raindata.txt"
        "data/layer_1.png"
        "data/layer_2.png"
        "data/layer_3.png"
        "data/oilrig.obj"
        "data/pipe.obj"
        "data/refinery.obj"
        "data/maptexture.jpg"
        "data/topography.png"
)

qt6_add_resources(RayCi "3D_View"
        PREFIX
        "/"
        FILES
        ${RayCi_resource_files}
)

install(TARGETS RayCi
        RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
        BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
        LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)


if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)


    if(EXISTS "${RAYCI_BIN_DIR}")
        file(GLOB RAYCI_BIN_FILES "${RAYCI_BIN_DIR}/*.dll")
        foreach(DLL_FILE ${RAYCI_BIN_FILES})
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${DLL_FILE}"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
        endforeach()
    endif()

    # FMC4030_DLL_DIR

    if(EXISTS "${FMC4030_BIN_DIR}")
        file(GLOB FMC4030_DLL_FILES "${FMC4030_BIN_DIR}/*.dll")
        foreach(DLL_FILE ${FMC4030_DLL_FILES})
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${DLL_FILE}"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
        endforeach()
    endif()

endif ()
